package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     *查询所有护理计划
     */
    @Override
    public List<NursingPlanVo> getAllPlan() {
        return nursingPlanMapper.selectAllNursingPlans();
    }

    /**
     *新增护理计划
     */
    @Override
    public void addPlan(NursingPlanDto nursingPlanDto) {

        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(0);
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            projectPlans. forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId().intValue());
            });
            nursingProjectPlanMapper.insertNursingProjectPlan(projectPlans);
        }

    }

    /**
     *条件分页查询护理计划
     */
    @Override
    public PageResponse getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> nursingPlanVos = nursingPlanMapper.selectByPage(name, status);
        return PageResponse.of(nursingPlanVos, NursingPlanVo.class);
    }

    /**
     *根据id查询护理计划
     */
    @Override
    public NursingPlanVo getById(Integer id) {
        return nursingPlanMapper.selectNursingPlanById(id);
    }

    /**
     *修改护理计划
     */
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);

        nursingProjectPlanMapper.deleteByIds(Collections.singletonList(nursingPlan.getId()));
        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId().intValue());
            });
            nursingProjectPlanMapper.insertNursingProjectPlan(projectPlans);
        }
    }

    /**
     *删除护理计划
     */
    @Override
    public void delete(Integer id){
        nursingPlanMapper.deleteNursingPlanById(id);
        nursingProjectPlanMapper.deleteByIds(Collections.singletonList(Long.valueOf(id)));

    }

    /**
     * 启用禁用护理计划
     */
    @Override
    public void openAndClose(Integer id, Integer status){
        nursingPlanMapper.statusControl(id,status);
    }
}
